require 'mina/multistage'
require 'mina/bundler'
require 'mina/rails'
require 'mina/git'
# require 'mina/rbenv'  # for rbenv support. (https://rbenv.org)
require 'mina/rvm'    # for rvm support. (https://rvm.io)
require 'mina/puma'

# Multistage settings:
set :stages, %w(production staging)
set :default_stage, 'staging'

# Basic settings:
set :domain, 'localhost'
set :deploy_to, '/web/mina_demo'
set :repository, 'git@gitee.com:sunbow/mina_demo.git'
set :branch, 'master'

# Optional settings:
#   set :user, 'foobar'          # Username in the server to SSH to.
#   set :port, '30000'           # SSH port number.
#   set :forward_agent, true     # SSH forward_agent.
set :application_name, 'mina_demo'
set :user, 'sunbow'
set :rails_env, 'staging'
set :ruby_version, 'ruby-2.4.5@default'

# Shared dirs and files will be symlinked into the app-folder by the 'deploy:link_shared_paths' step.
# Some plugins already add folders to shared_dirs like `mina/rails` add `public/assets`, `vendor/bundle` and many more
# run `mina -d` to see all folders and files already included in `shared_dirs` and `shared_files`
# set :shared_dirs, fetch(:shared_dirs, []).push('public/assets', 'public/uploads')
set :shared_dirs, fetch(:shared_dirs, []).push('tmp', 'log')
set :shared_files, fetch(:shared_files, []).push('config/database.yml', 'config/secrets.yml', 'config/application.yml', 'config/puma.rb')

# This task is the environment that is loaded for all remote run commands, such as
# `mina deploy` or `mina rake`.
task :remote_environment do
  # If you're using rbenv, use this to load the rbenv environment.
  # Be sure to commit your .ruby-version or .rbenv-version to your repository.
  # invoke :'rbenv:load'

  # For those using RVM, use this to load an RVM version@gemset.
  invoke :'rvm:use', fetch(:ruby_version)
end

# Put any custom commands you need to run at setup
# All paths in `shared_dirs` and `shared_paths` will be created on their own.
task :setup => :environment do
  # command %{rbenv install 2.3.0 --skip-existing}
  in_path(fetch(:deploy_to)) do
    command %{touch shared/config/database.yml}
    command %{touch shared/config/secrets.yml}
    command %{touch shared/config/application.yml}
    command %{touch shared/config/puma.rb}
    command %{mkdir -p shared/public/uploads}
    command %{mkdir -p shared/tmp/sockets}
    command %{mkdir -p shared/tmp/pids}
    command %{touch shared/tmp/sockets/puma.state}
  end
end

desc "Deploys the current version to the server."
task :deploy do
  # uncomment this line to make sure you pushed your local branch to the remote origin
  # invoke :'git:ensure_pushed'
  deploy do
    # Put things that will set up an empty directory into a fully set-up
    # instance of your project.
    invoke :'git:clone'
    invoke :'deploy:link_shared_paths'
    invoke :'bundle:install'
    invoke :'rails:db_migrate'
    invoke :'rails:assets_precompile'
    invoke :'deploy:cleanup'
    on :launch do
      in_path(fetch(:current_path)) do
        # command %{mkdir -p tmp/}
        # command %{touch tmp/restart.txt}
      end
      invoke :'puma:restart'
    end
  end

  # you can use `run :local` to run tasks on local machine before of after the deploy scripts
  # run(:local){ say 'done' }
end

# For help in making your deploy script, see the Mina documentation:
#
#  - https://github.com/mina-deploy/mina/tree/master/docs
